home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0155_Re: OLE Automation with EXCEL.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  2.1 KB  |  85 lines

  1.  
  2. {
  3. >I am interested in any info regarding the use of OLE Automation
  4. >in Delphi 2.0 . The demo programs provided with Delphi help
  5. >at first, but I seem to have come upon major stumbling blocks,
  6. >and the online help is skimpy on the subject. Specifically, I want
  7. >to communicate with Microsoft Excel. Executing the following
  8. >code:
  9. >
  10.  
  11. I don't remember where I got this but it seems to work for me in my exercises of learning OLE Auto
  12. with Excel:
  13. }
  14.  
  15. unit Excel1;
  16.  
  17. interface
  18.  
  19. uses
  20.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleAuto,
  21.   StdCtrls;
  22.  
  23. type
  24.   TForm1 = class(TForm)
  25.     Button1: TButton;
  26.     Button2: TButton;
  27.     bFancyAry: TButton;
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.     procedure bFancyAryClick(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.   public
  34.     { Public declarations }
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.DFM}
  43. var
  44.    V: Variant;
  45.  
  46. procedure TForm1.Button1Click(Sender: TObject);
  47. begin
  48.    V := CreateOleObject('Excel.Sheet');
  49.    V.Range('A1:D8').Formula := 'RAND()';
  50.    V.Application.Sheets.Add;
  51.    Caption := 'Num Sheets = ' + IntToStr(V.Application.Sheets.Count);
  52.    ShowMessage('Excel Sheet Created');
  53. end;
  54.  
  55. var
  56.    A: Variant;
  57. procedure TForm1.Button2Click(Sender: TObject);
  58. begin
  59.      A := CreateOleObject('Excel.Application'); //Start a new copy of Excel
  60.      A.Visible := True;
  61.      A.WorkBooks.Add;
  62.      A.Sheets.Add;
  63.      Caption := 'Num Sheets = ' + IntToStr(A.Sheets.Count);
  64. end;
  65.  
  66. procedure TForm1.bFancyAryClick(Sender: TObject);
  67. var
  68.    i, j: Integer;
  69.    Ch: Char;
  70.    SimpAry: Variant;
  71. begin
  72.      SimpAry := CreateOleObject('Excel.Application'); //Start a new copy Excel
  73.      SimpAry.Visible := True;
  74.      SimpAry.WorkBooks.Add;
  75.      SimpAry.Sheets.Add;
  76.      for i := 1 to 10 do
  77.          for j := 1 to 10 do
  78.              SimpAry.Cells[j, i].Value := i * j;
  79.      for i := 1 to 10 do begin
  80.          Ch := Chr(64 + i);
  81.          SimpAry.Cells[11, i].Value := Format('=Sum(%s1:%s10)', [Ch, Ch]);
  82. end;
  83. end;
  84. end.
  85.